<?php
namespace App\Http\Controllers\WebApi\Home;

use App\Http\Service\OrdersService;
use App\Repositories\Company\CompanyRepository;
use App\Repositories\JobsRepository;
use App\Services\Admin\CompanyService;
use App\Services\Admin\OrderBaleService;
use App\Services\Admin\SearchService;
use App\Services\Admin\StaffService;
use Illuminate\Http\Request;

class HomeController extends BaseController
{
    protected $bales; //货单
    protected $companys; //公司
    protected $facotrys; //工厂
    protected $workers; //人才
    protected $service;
    protected $companyRepository;

    public function __construct()
    {
        parent::__construct();
        $this->bales = new OrderBaleService();
        $this->companys = new CompanyService();
        $this->workers = new StaffService();
        $this->service = new SearchService();
        $this->companyRepository = new CompanyRepository();
        $this->jobsRepository = new JobsRepository();
        $this->ordersService = new OrdersService();
    }

    /**
     * 首页信息
     * @return array
     */
    public function getIndex()
    {
    //  exit('ddddd');
        //获取订单
        $bales = $this->bales->getWebIndex();
        //获取公司
        //$coms = $this->companys->getWebIndexCategory();
        $coms = [
            [
                'num'=>'50家',
                'type'=>'男女装企业'
            ],
            [
                'num'=>'10家',
                'type'=>'童装企业'
            ],
            [
                'num'=>'150家',
                'type'=>'鞋帽装企业'
            ],
            [
                'num'=>'500家',
                'type'=>'辅料装企业'
            ]
        ];

        //获取工厂
        $facs = $this->companys
            ->getWebIndexByCate(1);
        //获取人才
        $workers = $this->workers->getWebIndex();
        $dataArr = array(
            'bales' => $bales,
            'coms' => $coms,
            'facs' => $facs,
            'workers' => $workers,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 首页信息搜索
     * @return array
     */
    public function search(Request $request)
    {
        $uid = $request->get('loginUid');
        $keyword = htmlspecialchars(trim($request->input('keyword','')));
        $type = intval($request->input('type',1));
        $resData = $this->service->getApiIndex($keyword,$type);

        //设置用户搜索记录
        $this->service->setApiLog($uid,$keyword);

        return $this->success($resData);
    }


    /**
     * 货单列表
     * @return array
     */
    public function bales(Request $request)
    {
        $data['search'] = $this->ordersService->getSearchParame();

        $searchNum = $request->input('searchNum',0);
        $searchTime = $request->input('searchTime',0);
        $searchPrice = $request->input('searchPrice',0);
        $page = $request->input('page',1);
        $pageSize = $request->input('pagesize',20);

        //根据关键字拼接搜索条件
        $where = $this->ordersService->orderSqlWhere($searchNum,$searchTime,$searchPrice);

        $data['list'] = $this->ordersService->getBalesOrders($where,$page,$pageSize);
        return $this->output($data);
    }

    /**
     * 客户列表
     * @return array
     */
    public function customer(Request $request)
    {
        $cate = intval($request->input('cate'));

        $data = $this->companyRepository->getAllCompanyByCate($cate);

        return $this->output($data);
    }

    /**
     * 工厂列表
     * @return array
     */
    public function factory(Request $request)
    {
        $cate = intval($request->input('cate'));

        $data = $this->companyRepository->getAllCompanyByCate($cate);

        return $this->output($data);
    }

    /**
     * 工人列表
     * @return array
     */
    public function staff(Request $request)
    {
        $cate = intval($request->input('cate'));

        $staff = $this->jobsRepository->getDepartmentStaff($cate);

        return $this->output($staff);
    }

    /***/
    public function api(Request $request){
        echo ('afds');
    }
}